﻿#include "listwidget.h"
#include <QPainter>
#include <QScrollBar>

ListWidget::ListWidget(QWidget *parent)
    : QListWidget(parent), m_transparentBorder(false), m_backgroundColor(Qt::white),
      m_borderRadius(0), m_textFont("Segoe UI", 12), m_textColor(Qt::black), m_textSize(12),
      m_selectionColor(QColor(0, 120, 212)), m_hoverColor(QColor(229, 229, 229))
{
    // 初始化样式
    updateStyleSheet();

    // 设置滚动条样式
    verticalScrollBar()->setStyleSheet(
        "QScrollBar:vertical {"
        "   background-color: #F0F0F0;"
        "   width: 12px;"
        "   margin: 0px 0px 0px 0px;"
        "}"
        "QScrollBar::handle:vertical {"
        "   background-color: #C0C0C0;"
        "   min-height: 20px;"
        "   border-radius: 6px;"
        "}"
        "QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {"
        "   background: none;"
        "}"
        "QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {"
        "   background: none;"
        "}"
    );
}

void ListWidget::setTransparentBorder(bool transparent)
{
    m_transparentBorder = transparent;
    updateStyleSheet();
}

void ListWidget::setBackgroundColor(const QColor &color)
{
    m_backgroundColor = color;
    updateStyleSheet();
}

void ListWidget::setBorderRadius(int radius)
{
    m_borderRadius = radius;
    updateStyleSheet();
}

void ListWidget::setTextFont(const QFont &font)
{
    m_textFont = font;
    updateStyleSheet();
}

void ListWidget::setTextColor(const QColor &color)
{
    m_textColor = color;
    updateStyleSheet();
}

void ListWidget::setTextSize(int size)
{
    m_textSize = size;
    m_textFont.setPointSize(m_textSize);
    updateStyleSheet();
}

void ListWidget::setSelectionColor(const QColor &color)
{
    m_selectionColor = color;
    updateStyleSheet();
}

void ListWidget::setHoverColor(const QColor &color)
{
    m_hoverColor = color;
    updateStyleSheet();
}

void ListWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(viewport());

    // 绘制背景和圆角
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setBrush(m_backgroundColor);
    painter.setPen(Qt::NoPen);
    painter.drawRoundedRect(rect(), m_borderRadius, m_borderRadius);

    // 调用基类的paintEvent以绘制列表内容
    QListWidget::paintEvent(event);
}

void ListWidget::updateStyleSheet()
{
    QString styleSheet = QString(
        "QListWidget {"
        "   background-color: %1;"
        "   color: %2;"
        "   font-family: '%3';"
        "   font-size: %4pt;"
        "   border: %5;"
        "   border-radius: %6px;"
        "}"
        "QListWidget::item {"
        "   padding: 5px;"
        "   background-color: transparent;"
        "}"
        "QListWidget::item:hover {"
        "   background-color: %7;"
        "}"
        "QListWidget::item:selected {"
        "   background-color: %8;"
        "   color: white;"
        "}"
    ).arg(m_backgroundColor.name())
     .arg(m_textColor.name())
     .arg(m_textFont.family())
     .arg(m_textSize)
     .arg(m_transparentBorder ? "none" : "1px solid #CCCCCC")
     .arg(m_borderRadius)
     .arg(m_hoverColor.name())
     .arg(m_selectionColor.name());

    setStyleSheet(styleSheet);
}
